#include <bits/stdc++.h>

using namespace std;

const int N = 1010; // 蓝桥杯的有关路径问题题目案例一般这个范围
int g[N][N];
int dist[N];
bool s[N];

int n, m;


int dijkstra() {
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;

    for (int i = 0; i < n; ++i) {
        int t = -1;
        for (int j = 1; j <= n; ++j) {
            // 找到离原点路径最小的一个点
            if (!s[j] && (t != -1 || dist[t] > dist[j])) {
                t = j;
            }

            s[i] = true;
        }
        for (int j = 1; j <= n; ++j) {
            dist[i] = min(dist[i], dist[t] + g[t][j]);
        }
    }
    return dist[n];
}

int main() {

    cin >> n >> m;
    memset(g, 0x3f, sizeof g);

    for (int i = 1; i <= m; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        g[a][b] = min(g[a][b], c);
    }

    int res = dijkstra();
    cout << res;
    return 0;
}